const {getArrangeByUsername, newArrange, updateArrange} = require("../controller/arrange");
const {SuccessModel, ErrorModel} = require('../model/response')
var express = require('express');
const {updateCoachArrange} = require("../controller/coach");
var router = express.Router();

/**
 * 教练路由操作
 * Schedule.vue: /update, /new,
 */
// 添加练车时间安排
router.post('/new', async (req, res, next) => {
  // 修改教练练车时间安排
  const val = await newArrange(req.body)
  if (val) {
    const CoachVal = await updateCoachArrange(req.session.username)
    if (CoachVal) {
      res.json(new SuccessModel('练车时间安排成功'))
    }
  } else {
    res.json(new ErrorModel('练车时间安排失败'))
  }
})
// 修改练车时间安排
router.post('/update', (req, res, next) => {
  const result = updateArrange(req.body)
  return result.then(val => {
    if (val) {
      res.json(new SuccessModel('修改练车时间安排成功'))
    } else {
      res.json(new ErrorModel('修改练车时间安排失败'))
    }
  })
})

// 获取教练练车时间安排（未用到该路由）
router.post('/detail', (req, res, next) => {
  const result = getArrangeByUsername(req.session.username)
  return result.then(row => {
    res.json(new SuccessModel(row))
  })
})
module.exports = router
